<template>
  <div>
    <PageTemplate>
      <div slot="content">
        <PageEditCollapse :is-search="false" :default-collapse="true">
          <div slot="title" class="page-edit-colllapse-title">
            <div style="width: 200px;">{{$t('cm.baseinfo')}}</div>
          </div>
          <div>
            <!-- 表单 -->
            <el-form
              :model="phrase"
              label-position="right"
              label-width="150px"
              class="page-edit-colllapse-form"
              :rules="rules"
              ref="phrase"
            >
              <el-row>
                <el-col :span="12">
                  <el-form-item :label="$t('phrase.phrase_content')" prop="content">
                    <el-input v-model="phrase.content"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('phrase.indx')" prop="indx">
                    <el-input v-model="phrase.indx"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item :label="$t('phrase.valid')" prop="valid">
                    <el-radio v-model="phrase.valid" label="Y">是</el-radio>
                    <el-radio v-model="phrase.valid" label="N">否</el-radio>
                  </el-form-item>
                </el-col>
              </el-row>
              
            </el-form>
          </div>
        </PageEditCollapse>
        <el-row class="row-empty-10"></el-row>
        <el-row>
          <el-button type="primary" @click="submitForm('phrase')">{{$t('cm.commit')}}</el-button>
          <el-button type="primary" @click="toList">{{$t('cm.return')}}</el-button>
        </el-row>
      </div>
    </PageTemplate>
  </div>
</template>

<script>
import { ALERT } from '@/utils/responseHandle'
import * as Constant from '@/utils/constant'
import PageTemplate from '@/components/common/PageTemplate'
import PageEditCollapse from '@/components/common/PageEditCollapse'
import HasBtn from '@/mixins/HasBtn'
import { mapState } from 'vuex'
import { COMMON } from '@/utils/commonUtil'
import phrase from './js/phrase'
export default {
  components: {
    PageTemplate, PageEditCollapse
  },
  data () {
    return {
      phrase: {
        id: null,
        content: null,
        indx: null,
        valid: null
      },
      rules: {
        content: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        indx: [
          { required: true, message: '必填项,且为数字',  trigger: 'blur' }
        ],
        valid: [
          { required: true, message: '必填项', trigger: 'blur' }
        ]
      },
    }
  },
  created () {
    if (this.$router.history.current.params.id !== Constant.NEW_PARAMS) {
      this.phraseGet(this.$router.history.current.params.id)
    }
  },
  methods: {
    phraseGet (id) {
      phrase.phraseGet({ id }, result => {
        if (result.code === Constant.RT_SUCCESS) {
          this.phrase = result.data
        } else {
          // _this.$t('cm.tips')
          ALERT.error(result.msg)
        }
      })
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          phrase.phraseSave(this.phrase, result => {
            if (result.code === Constant.RT_SUCCESS) {
              ALERT.success(result.msg)
              this.toList()
            } else {
              // _this.$t('cm.tips')
              ALERT.error(result.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    toList () {
      COMMON.routerTo(Constant.ROUTE_PHRASE.index, this.$router)
    }
  },
  mounted () {
  },
  computed: {
  }
}
</script>

<style lang="scss">
</style>
